package cn.iocoder.yudao.module.member.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserGasAddressDO;
import cn.iocoder.yudao.module.member.service.user.CrmMemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 燃气户")
@RestController
@RequestMapping("/crm/member/user")
@Validated
@Slf4j
public class AppCrmMemberUserController {
    @Resource
    private CrmMemberUserService crmMemberUserService;


    @PostMapping("/create")
    @Operation(summary = "创建燃气户")
    @PreAuthenticated
    public CommonResult<Long> createMemberUser(@Valid @RequestBody AppCrmMemberUserCreateReqVO createReqVO, @RequestHeader Integer terminal) {
        return success(crmMemberUserService.createMemberUser(createReqVO, terminal));
    }

    @PutMapping("/update")
    @Operation(summary = "更新燃气户")
    @PermitAll
    public CommonResult<Boolean> updateMemberUser(@Valid @RequestBody AppCrmMemberUserUpdateReqVO updateReqVO) {
        crmMemberUserService.updateMemberUser(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得燃气户")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCrmMemberUserRespVO> getMemberUser(@RequestParam("id") Long id) {
        AppCrmMemberUserRespVO memberUser = crmMemberUserService.getMemberUserApp(id);
        return success(memberUser);
    }

    @GetMapping("/get/code")
    @Operation(summary = "自动生成燃气户code")
    @PermitAll
//    @Parameter(name = "key", description = "key表名或者业务名", required = true, example = "crm_id")
    public CommonResult<String> getCode() {
        //// 新生成规则
        String code = crmMemberUserService.getCodeByDate("crm_id");
        return success(code);
    }

    @GetMapping("/page")
    @Operation(summary = "获得燃气户分页")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<PageResult<AppCrmMemberUserRespVO>> getMemberUserPage(@Valid AppCrmMemberUserPageReqVO pageVO) {
        PageResult<AppCrmMemberUserRespVO> pageResult = crmMemberUserService.getAppMemberUserPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/get/salesmanname/list")
    @Operation(summary = "获得业务员下拉框列表")
    public CommonResult<PageResult<AppAdminUserRespDTO>> getSalesmanNameList(@Valid AppAdminUserRespDTO dto) {
        PageResult<AppAdminUserRespDTO> pageResult = crmMemberUserService.getAppSalesmanNameList(dto);
        return success(pageResult);
    }
}
